package com.hns.can.energy.web;
import java.net.URLEncoder;
import java.util.List;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.EnergyStatisticsService;
import com.hns.can.energy.vo.ParamVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.util.ResponseUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.enumrate.MsgType;
import com.hns.tool.export.ExportUtil;

import java.util.Arrays;

import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.PubUtil;

/**
 * 能耗统计信息Controller
 * @author Chenl
 *
 */
@Controller
@RequestMapping("/energyStatistics")
public class EnergyStatisticsController extends BaseController{
    @Autowired
    EnergyStatisticsService energyStatisticsService;

    final private String[] headerNames = new String[]{"数据日期","线路名称","车牌号","车辆编号","设备编码","燃料类型","记录数",
            "里程为0数","能耗为0数","时长为0数","最早数据时间","最晚数据时间","最小里程","最大里程","差值里程","叠加里程","当日区间里程",
            "GPS里程", "前一日补偿里程", "后一日补偿里程","最终里程","最小能耗","最大能耗","差值能耗","叠加能耗","当日区间能耗",
			"补偿能耗", "最终能耗", "最小运行时长","最大运行时长","差值时长","叠加时长","当日区间时长"};
    final private String[] headerFields = new String[]{ "rcrdDate", "lineName", "licPltNo","carInCd","eqmtCode", "fuelType",
            "rowCount","zeroMileCount", "zeroEnerCount", "zeroOprtCount", "minRcrdTime","maxRcrdTime", "minTotalMile",
            "maxTotalMile", "maxRngMile",  "sumRngMile",  "commMaxRngMile", "gpsMile", "buMile1", "buMile2","commMileBu", 
            "minTotalEnerCons", "maxTotalEnerCons", "maxRngEnerCons","sumRngEnerCons","commMaxRngEnerCons",
			"buEne", "commEneBu", "minTotalOprtDura","maxTotalOprtDura", "maxRngOprtDura", "sumRngOprtDura", "commMaxRngOprtDura"};

    /**
     * 查询
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchEnergyStatistics")
    @ResponseBody
    public Object searchEnergyStatistics(ParamVo vo) throws Exception {
        Pagination page = energyStatisticsService.searchEnergyStatistics(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 获取能耗明细
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/getCarEnergyDetail")
    @ResponseBody
    public Object getCarEnergyDetail(CommonParamsVo vo) throws Exception {
            List list = energyStatisticsService.getCarEnergyDetail(vo);
            return this.getReturnResult(list);
        }

    /**
     * 能耗表统计信息导出excel
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportEnergyStatisticsExcel")
    public void exportEnergyStatisticsExcel(ParamVo vo, HttpServletResponse response) throws Exception {
        List list = energyStatisticsService.exportEnergyStatistics(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
                Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("能耗统计信息", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }

    /**
     * 能耗表统计信息导出PDF
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportEnergyStatisticsPdf")
    public void exportEnergyStatisticsPdf(ParamVo vo, HttpServletResponse response) throws Exception {
        List list = energyStatisticsService.exportEnergyStatistics(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
                Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("能耗统计信息", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));

        ExportUtil.generatorPDF(pdfEntity, response);
    }
    
    
}
